<template>
  <el-card class="cate-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 添加按钮 -->
      <el-row type="flex" justify="end">
        <el-col :span="2"><el-button type="primary" size="medium" @click="AddDialogVisible = true">添加分类</el-button></el-col>
      </el-row>
    <!-- 文章列表 -->
    <el-table :data="cateList" border style="width: 100%" stripe >
      <el-table-column type="index" label="序号" ></el-table-column>
      <el-table-column prop="cate_name" label="分类名称" ></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          {{row}}
            <el-button type="primary" size="mini" @click="showEditDialog(row.id)">修改</el-button>
            <el-button type="danger" size="mini" @click="delCate(row.id)">删除</el-button>
          </template>
      </el-table-column>
   </el-table>
   <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible="AddDialogVisible"
      width="35%"
      >
       <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AddDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">添 加</el-button>
      </span>
    </el-dialog>

    <!-- 修改分类的对话框 -->
    <el-dialog
      title="修改文章分类"
      :visible="EditDialogVisible"
      width="35%"
      @close="EditDialogVisible = false"
      >
       <!-- 修改分类的表单 -->
      <el-form :model="editForm" :rules="addRules" ref="editRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="EditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateCateInfo">确定</el-button>
      </span>
    </el-dialog>

  </el-card>
</template>

<script>
// 导入vuex中的辅助函数
import { mapActions, mapState } from 'vuex'
// 调用接口
import { reqAddArticeCate, reqDelArticleCate, reqGetCateInfoById, reqUpdateCateInfo } from '@/api/article'
export default {
  data () {
    return {
      // 控制弹框的显示与隐藏 true / false
      AddDialogVisible: false,
      // 添加的表单
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      // 控制修改分类的对话框组件
      EditDialogVisible: false,
      // 修改的表单
      editForm: {
        cate_name: '',
        cate_alias: ''
      }

    }
  },
  created () {
    // 调用获取文章分类的方法
    this.getCateList()
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  methods: {
    // 映射获取文章分类的action
    ...mapActions('article', ['getCateList']),
    // 关闭添加分类对话框的方法
    resetAddForm () {
      // 1.关闭对话框
      this.AddDialogVisible = false
      // 2.重置表单数据和校验规则
      this.$refs.addRef.resetFields()
    },

    // 添加文章分类
    addCate () {
      // 1预校验
      this.$refs.addRef.validate(async boo => {
        if (!boo) return console.log('请填写')
        // 2携带参数发送请求
        const { data: { code, message } } = await reqAddArticeCate(this.addForm)
        // 提示用户
        if (code !== 0) return this.$message.info(message)
        this.$message.success(message)
        // 4关闭对话框
        this.AddDialogVisible = false
        // 5重新获取文章列表
        this.getCateList()
      })
    },
    // 删除文章
    async delCate (id) {
      // 判断要删除的是否为前两个分类
      if (id === 1 || id === 2) return this.$message.warning('管理员不允许删除前两个分类！')
      // 1.确认id
      // console.log(id)
      // 2确认消息提示
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', { type: 'warning' }).catch(() => {})
      // console.log(res)
      if (res !== 'confirm') return this.$message.info('已取消操作')
      // 3发送请求
      const { data } = await reqDelArticleCate(id)
      // 4.提示用户失败走这里
      if (data.code !== 0) return this.$message.error('删除文章分类失败')
      // 5删除成功
      this.$message.success(data.message)
      // 6重新渲染页面  调用文章分类
      this.getCateList()
    },
    async showEditDialog (id) {
      console.log(id)
      // 禁止修改1/2项
      if (id === 1 || id === 2) return this.$message.warning('管理员不允许修改该分类！')
      // 点击修改弹出对话框
      this.EditDialogVisible = true
      // 发送请求
      const { data } = await reqGetCateInfoById(id)
      console.log(data)
      // 数据回显
      if (data.code !== 0) return this.$message.error('获取分类失败')
      this.editForm = data.data
    },
    // 更新文章分类
    updateCateInfo () {
      // 1. 预校验
      this.$refs.editRef.validate(async boo => {
        if (!boo) return console.log('填写完整信息')
        // 2. 发送请求
        const { data } = await reqUpdateCateInfo(this.editForm)
        // 3. 提示
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        // 4. 关闭对话框
        this.EditDialogVisible = false
        // 5. 重新渲染
        this.getCateList()
      })
    }
  }
}
</script>

<style>

</style>
